<template>
    <div class="block" style="margin-left: 50px;">
        <el-row>
            <el-col :span="24" class="qryform">
                <el-button type="success" @click="setType(1)">拉取港通数据</el-button>
                <el-button type="success" @click="setType(4)">拉取日背离数据</el-button>
                <el-button type="success" @click="setType(5)">拉取周背离数据</el-button>
                <el-button type="success" @click="setType(6)">拉取跳价数据</el-button>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24" class="qryform">
                <el-button type="warning" @click="fillRecord">填充记录</el-button>
            </el-col>
        </el-row>
        <el-dialog  v-dialogDrag
            :title="name+'数据拉取'"
            :visible.sync="dialogVisible"
            :close-on-click-modal = "false"
            width="600px">
            <el-form>
                <el-form-item label="当前状态" :class="isPulling?'red':'green'">
                    {{isPulling?"正在拉取":"空闲"}}
                </el-form-item>
                <el-form-item  v-if="info">
                    {{info}}
                </el-form-item>
                <!-- <el-form-item label="选择日期">
                    <el-date-picker v-model="pullDate" type="date" placeholder="选择日期" :clearable="false"
                        value-format="yyyy-MM-dd" format="yyyy-MM-dd"></el-date-picker>
                </el-form-item> -->
                <el-form-item style="text-align:center">
                    <el-button type="primary" @click="startPull">开始拉取</el-button>
                    <el-button type="primary" v-if="isPulling" @click="stopPull">强行停止</el-button>
                    <el-button @click="dialogVisible = false">关闭</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>


<script>
import config from '~/config'
import dayjs from '~/plugins/day';

import datadict from '~/plugins/dict';

export default {
    data() {
        return {
            type : 1,
            name : "",

            loading: false,
            dialogVisible : false,
            isPulling : false,
            info : "",
            pullDate : "",

            eventlist: datadict.eventTypeList,
            eventMap : datadict.getEventTypeMap(),
        }
    },
     

    mounted() {
        var that = this;
        var date = new Date();
        date.setTime(date.getTime() - 3600 * 1000 * 24);
        this.pullDate = dayjs(date).format();
        setInterval(function(){
            if(!that.dialogVisible) return;
            that.getPullStatus();

        },3000)
    },

    methods: {
        setType(type){
            this.type = type;
            this.dialogVisible = true;
            this.name = this.eventMap[type];
        },
        getPullStatus() {
            this.$axios.get(`${config.sapi}/api/stock/getGTTaskStatus`)
            .then((res) => {
                if (res.data.code == 200) {
                    this.isPulling = res.data.data.status;
                    this.info = res.data.data.info;
                } else {
                    this.$message.error(res.data.message);
                }
            })

        },
        stopPull() {
            this.$axios.get(`${config.sapi}/api/stock/stopGTTask`)
            .then((res) => {
                if (res.data.code == 200) {
                } else {
                    this.$message.error(res.data.message);
                }
            })

        },
        startPull() {
            // var date = this.pullDate;
            // date=date.substring(0,10).replace(/-/g,"")
            this.$axios.get(`${config.sapi}/api/stock/getNewEventList?type=${this.type}`)
            .then((res) => {
                if (res.data.code == 200) {
                    this.isPulling = res.data.data;
                } else {
                    this.$message.error(res.data.message);
                }
            })

        },
        fillRecord(){
            this.$axios.get(`${config.sapi}/api/stock/fillRecord`)
            .then((res) => {
                if (res.data.code == 200) {
                } else {
                    this.$message.error(res.data.message);
                }
            })
        }
    }
}
</script>

<style lang="less" scoped>
.flexdiv {
    display: flex;
    justify-content: flex-start;
    margin-bottom: 10px;
    button{
        margin-left: 10px;
    }
}

.qryform{
    margin-bottom:10px;
    button{
        margin-top:0px;
        vertical-align: middle;
    }
}
.order-class{
    display: inline-block;
    margin-right: 10px;
    min-width: 50px;
}

.red{
    color: red;
}
.green{
    color: green;
}
.yellow{
    color: rgb(96, 22, 233)
}
.link-item{
    font-size: 12px;
}

</style>
